package provider.gateway;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import libra.model.JsonResult;

/**
 * 网关API - GateWay专用测试Predicate断言Controller
 * @author libra
 */
@RestController
@RequestMapping("gw/predicate")
public class GatewayPredicateController extends BaseController {
	@Value( "#{'${server.servlet.context-path}'.concat('[port(${server.port})]')}" )
	String identity;
	
	private JsonResult<Object> buildSuccResp( Object obj ){
		return new JsonResult<Object>(0, identity, obj);
	}
	
	// 测试Gateway.Predicate的 -After 断言，判断当前请求日期是否晚于指定日期
	@RequestMapping("afterTime")
	public Object afterTime( HttpServletRequest req ) throws Exception{
		return buildSuccResp("afterTime");
	}
	// 测试Gateway.Predicate的 -Before 断言，判断当前请求日期是否早于指定日期
	@RequestMapping("beforeTime")
	public Object beforeTime( HttpServletRequest req ) throws Exception{
		return buildSuccResp("beforeTime");
	}
	// 测试Gateway.Predicate的 -Between 断言，判断当前请求日期是否介于指定起始日期、结束日期之间
	@RequestMapping("betweenTime")
	public Object betweenTime( HttpServletRequest req ) throws Exception{
		return buildSuccResp("betweenTime");
	}
	
	// 测试Gateway.Predicate的 -Path 断言，测试条件：/path1/**，判断当前请求请求路径是否与配置值匹配
	@RequestMapping("path1/a")
	public Object path1_a( HttpServletRequest req ) throws Exception{
		return buildSuccResp("path1_a");
	}
	// 测试Gateway.Predicate的 -Path 断言，测试条件：/path1/**，判断当前请求请求路径是否与配置值匹配
	@RequestMapping("path1/b")
	public Object path1_b( HttpServletRequest req ) throws Exception{
		return buildSuccResp("path1_b");
	}
	// 测试Gateway.Predicate的 -Path 断言，测试条件：/path2/{p}，判断当前请求请求路径是否与配置值匹配
	@RequestMapping("path2/{param}")
	public Object betweenTime( @PathVariable("param")String param, HttpServletRequest req ) throws Exception{
		return buildSuccResp("path2/{param} -> " + param);
	}
	
	
	@RequestMapping("cookie")
	public Object cookie( HttpServletRequest req ) throws Exception{
		return buildSuccResp( new HashMap<>() {{
			put("allCookies", getAllCookies(req));
		}} );
	}
	
	
	@RequestMapping("header")
	public Object header( HttpServletRequest req ) throws Exception{
		return buildSuccResp( new HashMap<>() {{
			put("allHeaders", getAllHeaders(req));
		}} );
	}
	
	
	@RequestMapping("method")
	public Object method( HttpServletRequest req ) throws Exception{
		Map<String, String> map = new HashMap<String, String>();
		map.put("methodType", req.getMethod() );
		return buildSuccResp( map );
	}
	
	
	@RequestMapping("query")
	public Object query( HttpServletRequest req ) throws Exception{
		return buildSuccResp(new HashMap<>() {{
			put("queryParams", getAllParams(req));
		}} );
	}
	
	
	@RequestMapping("remoteAddr")
	public Object remoteAddr( HttpServletRequest req ) throws Exception{
		return buildSuccResp(new HashMap<>() {{
			put("IP地址", getIpAddress(req));
		}} );
	}
	
	
	@RequestMapping("host")
	public Object host( HttpServletRequest req ) throws Exception{
		return buildSuccResp(new HashMap<>() {{
			put("host域名", req.getServerName());
		}} );
	}
	
	
	
	@RequestMapping("selfCustom")
	public Object selfCustom( HttpServletRequest req ) throws Exception{
		Map<String, String> map = new HashMap<String, String>();
		map.put("请求方式", req.getMethod());
		map.put("请求URL", req.getRequestURI());
		map.put("指定请求参数", "customPara->"+req.getParameter("customPara"));
		return buildSuccResp(new HashMap<>() {{
			put("自定义断言", map);
		}} );
	}
	
	@RequestMapping("selfCustom2")
	public Object selfCustom2( @RequestHeader("selfHeader")String headerVal, HttpServletRequest req ) throws Exception{
		Map<String, String> map = new HashMap<String, String>();
		map.put("请求方式", req.getMethod());
		map.put("请求URL", req.getRequestURI());
		map.put("指定请求头", "selfHeader->"+headerVal);
		map.put("指定请求参数", "selfParam->"+req.getParameter("selfParam"));
		return buildSuccResp(new HashMap<>() {{
			put("自定义断言", map);
		}} );
	}
}
